package com.wutao.panzh.admin.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.panzh.entity.InformationItem;
import com.panzh.entity.index.IndexInformationItem;
import com.panzh.myconst.ProjectParames;
import com.wutao.panzh.admin.mapper.InformationItemMapper;
import com.wutao.panzh.service.InforMationItemService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@Service
public class InforMationItemServiceImpl implements InforMationItemService {

    @Autowired
    private InformationItemMapper informationItemMapper;

    @Override
    public InformationItem getInformationItemById(String InformationId) {
        InformationItem informationItem = new InformationItem();
        informationItem.setItemId(Integer.parseInt(InformationId));
        return informationItemMapper.selectOne(informationItem);
    }

    @Override
    public void deleteInformationById(String informationId) {
        Integer integer = Integer.parseInt(informationId);
        informationItemMapper.deleteInformationImage(informationId);
        informationItemMapper.deleteByPrimaryKey(integer);
    }

    //实现分页的service方法
    @Override
    public PageInfo<InformationItem> getInformationItemPageInfo(Integer pageNo, Integer pageSize, String keyWord) {
        //使用PageHelper启动分页；
        PageHelper.startPage(pageNo,pageSize);
        //一定要注意pageHelper的版本问题，写文档的时候进行查询
        //https://blog.csdn.net/qq_31204567/article/details/78172168；该网站会有你想要的信息
        List<InformationItem> informationItems = informationItemMapper.selectFroSearch(keyWord);
        //封装成PageInfo进行返回
        PageInfo<InformationItem> pageInfo = new PageInfo<>(informationItems);
        return pageInfo;
    }

    //根据itemName来查询；
    @Override
    public InformationItem getInformatinByItemName(String itemName) {
        InformationItem informationItem = new InformationItem();
        informationItem.setItemName(itemName);
        return informationItemMapper.selectOne(informationItem);
    }

    @Override
    public void saveInformation(InformationItem informationItem) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = simpleDateFormat.format(date);
        informationItem.setItemData(format);
        informationItemMapper.insert(informationItem);
    }

    @Override
    public void updateInformation(InformationItem informationItem) {
        //直接调用通用mapper进行数据更新，但是修改时间也要进行更新；
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format = simpleDateFormat.format(date);
        informationItem.setItemData(format);
        informationItemMapper.updateByPrimaryKey(informationItem);
    }

    @Override
    public List<InformationItem> getInformatinAllByItemName(String itemName) {
        InformationItem informationItem = new InformationItem();
        informationItem.setItemName(itemName);
        List<InformationItem> informationItems = informationItemMapper.select(informationItem);
        return informationItems;
    }

    @Override
    public void insertInformationImageSrc(String imagSrc, String informationItemId) {
        //先进行查询，如果查询有结果，就进行更新，如果查询没有结果，就进行插入；
        String imagSrcByInformationItemId = informationItemMapper.selectImagSrcByInformationItemId(informationItemId);
        if (imagSrcByInformationItemId == null){
            //进行插入；
            informationItemMapper.insertImagSrcByInformationItemId(imagSrc,informationItemId);
        }else {
            //进行更新；
            informationItemMapper.updateImagSrcByInformationItemId(imagSrc,informationItemId);
        }
    }

    @Override
    public PageInfo<IndexInformationItem> getAllIndexInformation(Integer pageNo) {
        //使用PageHelper启动分页；
        PageHelper.startPage(pageNo, ProjectParames.PAGE_SIZE);
        //一定要注意pageHelper的版本问题，写文档的时候进行查询
        //https://blog.csdn.net/qq_31204567/article/details/78172168；该网站会有你想要的信息
        List<IndexInformationItem> informationItems = informationItemMapper.selectIndexInformationFroSearch();
        //封装成PageInfo进行返回
        PageInfo<IndexInformationItem> pageInfo = new PageInfo<>(informationItems);
        return pageInfo;
    }
}
